var hash = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function getToken(s) {
    if (/([^\u0000-\u00ff])/.test(s)) {
        throw new Error('INVALID_CHARACTER_ERR');
    }
    var i = 0,
        prev,
        ascii,
        mod,
        result = [];

    while (i < s.length) {
        ascii = s.charCodeAt(i);
        mod = i % 3;

        switch (mod) {

        case 0:
            result.push(hash.charAt(ascii >> 2));
            break;

        case 1:
            result.push(hash.charAt((prev & 3) << 4 | (ascii >> 4)));
            break;

        case 2:
            result.push(hash.charAt((prev & 0x0f) << 2 | (ascii >> 6)));
            result.push(hash.charAt(ascii & 0x3f));
            break;
        }

        prev = ascii;
        i++;
    }

    if (mod == 0) {
        result.push(hash.charAt((prev & 3) << 4));
        result.push('==');
    } else if (mod == 1) {
        result.push(hash.charAt((prev & 0x0f) << 2));
        result.push('=');
    }

    return result.join('');
}